﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shop.Data
{
    public class ShopData
    {
        public static void UpdateShopItem(string barcode,decimal price)
        {
            ShopDataClassesDataContext shopDB = new ShopDataClassesDataContext();
            ShopItem foundItemByBarcode = (from s in shopDB.GetTable<ShopItem>()
                                             where s.Barcode.Equals(barcode)
                                             select s).SingleOrDefault();
            if (foundItemByBarcode.Quantity == null)
            {
                foundItemByBarcode.Weight -= price / foundItemByBarcode.Price;
            }
            else
            {
                foundItemByBarcode.Quantity -= (int?)(price / foundItemByBarcode.Price);
            }
            shopDB.SubmitChanges();
        }

        public static void AddNewProduct(string type, int typeNumber, string barcode, string spec, decimal? weight,
                                         int? quantity, DateTime expDate, decimal price)
        {
            ShopDataClassesDataContext shopDB = new ShopDataClassesDataContext();
            ShopItem newItem = new ShopItem();
            newItem.Type = type;
            newItem.Specification = spec;
            newItem.TypeNumber = typeNumber;
            newItem.Barcode = barcode;
            newItem.Weight = weight;
            newItem.Quantity = quantity;
            newItem.ExpDate = expDate;
            newItem.Price = price;
            shopDB.ShopItems.InsertOnSubmit(newItem);
            shopDB.SubmitChanges();
        }

        public static bool IsBarcodeFound(string barcode)
        {
            ShopDataClassesDataContext shopDB = new ShopDataClassesDataContext();
            var barcodeFound = (from s in shopDB.GetTable<ShopItem>()
                                where s.Barcode.Equals(barcode)
                                select s).ToArray<ShopItem>();
            if (barcodeFound.Length > 0)
            {
                return true;
            }
            return false;
        }

        public static void EditProduct(string oldBarcode, string type, int typeNumber, string newBarcode, string spec, decimal? weight,
            int? quantity, DateTime expDate, decimal price)
        {
            ShopDataClassesDataContext shopDB = new ShopDataClassesDataContext();
            ShopItem foundItemByBarcode = (from s in shopDB.GetTable<ShopItem>()
                                           where s.Barcode.Equals(oldBarcode)
                                           select s).First<ShopItem>();
            foundItemByBarcode.Type = type;
            foundItemByBarcode.Specification = spec;
            foundItemByBarcode.TypeNumber = typeNumber;
            if (oldBarcode != newBarcode)
            {
                foundItemByBarcode.Barcode = newBarcode;
            }
            foundItemByBarcode.Weight = weight;
            foundItemByBarcode.Quantity = quantity;
            foundItemByBarcode.ExpDate = expDate;
            foundItemByBarcode.Price = price;
            shopDB.SubmitChanges();
        }

        public static ShopItem FindItemByBarcode(string barcode)
        {
            ShopDataClassesDataContext shopDB = new ShopDataClassesDataContext();
            ShopItem[] foundItemByBarcode = (from s in shopDB.GetTable<ShopItem>()
                                             where s.Barcode.Equals(barcode)
                                             select s).ToArray<ShopItem>();
            if (foundItemByBarcode.Length == 0)
            {
                return null;
            }
            return foundItemByBarcode[0];
        }

        public static IQueryable GetAllProducts()
        {
            ShopDataClassesDataContext shopDB = new ShopDataClassesDataContext();
            var allProducts = from s in shopDB.GetTable<ShopItem>()
                              select new { s.Barcode, s.Type, s.Specification, s.Price, s.Quantity, s.Weight, s.ExpDate };
            return allProducts;
        }

        public static IQueryable GetProductsByExpDate(DateTime expDate)
        {
            ShopDataClassesDataContext shopDB = new ShopDataClassesDataContext();
            var productsByExpDate = from s in shopDB.GetTable<ShopItem>()
                                    where s.ExpDate < expDate
                                    select new { s.Barcode, s.Type, s.Specification, s.Price, s.Quantity, s.Weight, s.ExpDate };
            return productsByExpDate;
        }

        public static IQueryable GetProductsByPrice(decimal startPrice, decimal endPrice)
        {
            ShopDataClassesDataContext shopDB = new ShopDataClassesDataContext();
            var productsByPrice = from s in shopDB.GetTable<ShopItem>()
                                  select new { s.Barcode, s.Type, s.Specification, s.Price, s.Quantity, s.Weight, s.ExpDate };
            if (startPrice != 0)
            {
                productsByPrice = from s in productsByPrice
                                  where s.Price >= startPrice
                                  select new { s.Barcode, s.Type, s.Specification, s.Price, s.Quantity, s.Weight, s.ExpDate };
            }
            if (endPrice != 0)
            {
                productsByPrice = from s in productsByPrice
                                  where s.Price <= endPrice
                                  select new { s.Barcode, s.Type, s.Specification, s.Price, s.Quantity, s.Weight, s.ExpDate };
            }

            return productsByPrice;
        }

        public static IQueryable GetProductsByQuantity(decimal startQuantity, decimal endQuantity)
        {
            ShopDataClassesDataContext shopDB = new ShopDataClassesDataContext();
            var productsByQuantity = from s in shopDB.GetTable<ShopItem>()
                                     select new { s.Barcode, s.Type, s.Specification, s.Price, s.Quantity, s.Weight, s.ExpDate };
            if (startQuantity != 0)
            {
                productsByQuantity = from s in productsByQuantity
                                     where s.Quantity >= startQuantity && s.Weight == null
                                     select new { s.Barcode, s.Type, s.Specification, s.Price, s.Quantity, s.Weight, s.ExpDate };
            }
            if (endQuantity != 0)
            {
                productsByQuantity = from s in productsByQuantity
                                     where s.Quantity <= endQuantity && s.Weight == null
                                     select new { s.Barcode, s.Type, s.Specification, s.Price, s.Quantity, s.Weight, s.ExpDate };
            }

            return productsByQuantity;
        }

        public static IQueryable GetProductsByWeight(decimal startWeight, decimal endWeight)
        {
            ShopDataClassesDataContext shopDB = new ShopDataClassesDataContext();
            var productsByWeight = from s in shopDB.GetTable<ShopItem>()
                                     select new { s.Barcode, s.Type, s.Specification, s.Price, s.Quantity, s.Weight, s.ExpDate };
            if (startWeight != 0)
            {
                productsByWeight = from s in productsByWeight
                                     where s.Quantity >= startWeight && s.Quantity == null
                                     select new { s.Barcode, s.Type, s.Specification, s.Price, s.Quantity, s.Weight, s.ExpDate };
            }
            if (endWeight != 0)
            {
                productsByWeight = from s in productsByWeight
                                     where s.Quantity <= endWeight && s.Quantity == null
                                     select new { s.Barcode, s.Type, s.Specification, s.Price, s.Quantity, s.Weight, s.ExpDate };
            }

            return productsByWeight;
        }

        public static IQueryable GetProductsBySpecification(string spec)
        {
            ShopDataClassesDataContext shopDB = new ShopDataClassesDataContext();
            var productsBySpec = from s in shopDB.GetTable<ShopItem>()
                                 where s.Specification.Contains(spec)
                                 select new { s.Barcode, s.Type, s.Specification, s.Price, s.Quantity, s.Weight, s.ExpDate };
            return productsBySpec;
        }

        public static IQueryable GetProductsByType(int typeNumber)
        {
            ShopDataClassesDataContext shopDB = new ShopDataClassesDataContext();
            var productsByType = from s in shopDB.GetTable<ShopItem>()
                                 where s.TypeNumber == typeNumber
                                 select new { s.Barcode, s.Type, s.Specification, s.Price, s.Quantity, s.Weight, s.ExpDate };
            return productsByType;
        }
    }
}
